/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadPermissaoUsuario.java
 *
 * Created on 25/11/2011, 11:30:31
 */
package visao;

import fachada.FachadaBDR;
import funcionario.Funcionario;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import login.Login;
import permissao.Permissao;
import permissaoUsuario.PermissaoUsuario;
import util.CamposObrigatoriosException;
import util.ErroAcessoRepositorioException;
import util.KeyValue;

/**
 *
 * @author Administrador
 */
public class CadPermissaoUsuario extends javax.swing.JFrame {
    
    
    /** Creates new form CadPermissaoUsuario */
    public CadPermissaoUsuario() {}
    
    /**
     * Método construtor para cadastro de permissão usuario
     * @param idFuncionario 
     */
    public CadPermissaoUsuario(int idFuncionario) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/PermissaoUsuario.gif")).getImage());
        
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // recuperando o funcionario
            Funcionario objFuncionario = objFachadaBDR.procurarFuncionario(idFuncionario);
            // setando o valor recuperado
            jComboBox_idFuncionario.setSelectedItem(new KeyValue(objFuncionario.getNome(), String.valueOf(objFuncionario.getIdFuncionario())));
            
            jComboBox_idFuncionario.setEnabled(false);
        } catch (Exception ex) {
            jLabel_Mesagem.setText(ex.getMessage());
        }
    }
    
    /**
     * Método construtor para edição
     * @param idFuncionario
     * @param idPermissaoUsuario 
     */
    public CadPermissaoUsuario(int idFuncionario, int idPermissaoUsuario) {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setIconImage(new ImageIcon(getClass().getResource("/Imagens/PermissaoUsuario.gif")).getImage());
        
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            // recuperando o funcionario
            Funcionario objFuncionario = objFachadaBDR.procurarFuncionario(idFuncionario);
            PermissaoUsuario objPermissaoUsuario = objFachadaBDR.procurarPermissaoUsuario(idPermissaoUsuario);
            Permissao objPermissao = objPermissaoUsuario.getPermissao();
            
            // setando o valor recuperado
            id_permissao_usuario.setText(Integer.toString(objPermissaoUsuario.getIdPremissaoUsuario()));
            jComboBox_idFuncionario.setSelectedItem(new KeyValue(objFuncionario.getNome(), String.valueOf(objFuncionario.getIdFuncionario())));
            jComboBox_idPermissao.setSelectedItem(new KeyValue(objPermissao.getDescricaoPermissao(), Integer.toString(objPermissao.getIdPermissao())));
            
            jComboBox_idFuncionario.setEnabled(false);
        } catch (Exception ex) {
            jLabel_Mesagem.setText(ex.getMessage());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel_Codigo = new javax.swing.JLabel();
        jLabel_Funcionario = new javax.swing.JLabel();
        jLabel_Permissao = new javax.swing.JLabel();
        jComboBox_idFuncionario = new javax.swing.JComboBox();
        jComboBox_idPermissao = new javax.swing.JComboBox();
        jButton_Ok = new javax.swing.JButton();
        jButton_Cancelar = new javax.swing.JButton();
        jLabel_Mesagem = new javax.swing.JLabel();
        id_permissao_usuario = new javax.swing.JTextField();

        setTitle("Permissão Usuario");
        setResizable(false);

        jLabel_Codigo.setText("Codigo :");

        jLabel_Funcionario.setText("Funcionario :");

        jLabel_Permissao.setText("Permissão :");

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            List<Funcionario> listFuncionario = objFachadaBDR.listarFuncionario("");
            jComboBox_idFuncionario.addItem(new KeyValue("--- Favor Selecionar ---", "0"));
            for (Funcionario objFuncionario : listFuncionario) {
                jComboBox_idFuncionario.addItem(new KeyValue(objFuncionario.getNome(),String.valueOf(objFuncionario.getIdFuncionario())));
            }
        } catch (Exception e) {
        }

        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();
            List<Permissao> listPermissao = objFachadaBDR.listarPermissao("");
            jComboBox_idPermissao.addItem(new KeyValue("--- Favor Selecionar ---", "0"));
            for (Permissao objPermissao : listPermissao) {
                jComboBox_idPermissao.addItem(new KeyValue(objPermissao.getDescricaoPermissao(), Integer.toString(objPermissao.getIdPermissao())));
            }
        } catch (Exception e) {
        }

        jButton_Ok.setText("Ok");
        jButton_Ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_OkActionPerformed(evt);
            }
        });

        jButton_Cancelar.setText("Cancelar");
        jButton_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelarActionPerformed(evt);
            }
        });

        id_permissao_usuario.setText("0");
        id_permissao_usuario.setEnabled(false);
        id_permissao_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                id_permissao_usuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Ok, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Funcionario, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel_Permissao, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox_idFuncionario, 0, 320, Short.MAX_VALUE)
                            .addComponent(jComboBox_idPermissao, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addComponent(jLabel_Mesagem, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel_Codigo)
                        .addGap(18, 18, 18)
                        .addComponent(id_permissao_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_Codigo)
                    .addComponent(id_permissao_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Funcionario)
                    .addComponent(jComboBox_idFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Permissao)
                    .addComponent(jComboBox_idPermissao, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton_Ok, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton_Cancelar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel_Mesagem, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_OkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_OkActionPerformed
        // TODO add your handling code here:
        int idPermissaoUsuario = Integer.parseInt(id_permissao_usuario.getText());
        int idFuncionario = Integer.parseInt(((KeyValue) jComboBox_idFuncionario.getSelectedItem()).getValue());
        int idPermissao = Integer.parseInt(((KeyValue) jComboBox_idPermissao.getSelectedItem()).getValue());
        
        try {
            FachadaBDR objFachadaBDR = FachadaBDR.getInstancia();

            Funcionario func = objFachadaBDR.procurarFuncionario(idFuncionario);
            Permissao permissao = objFachadaBDR.procurarPermissao(idPermissao);
            PermissaoUsuario permissaoUsuario = new PermissaoUsuario(idPermissaoUsuario, permissao, func.getObjLogin());
            if(idPermissaoUsuario == 0){
                objFachadaBDR.cadastrarPermissaoUsuario(permissaoUsuario);
                jComboBox_idPermissao.setSelectedIndex(0);                    
            }
            else
                objFachadaBDR.atualizarPermissaoUsuario(permissaoUsuario);
            
            String acao = (idPermissaoUsuario == 0) ? "Cadastrada" : "Atualizada";
            jLabel_Mesagem.setText("Permissão Usário " + acao + " com sucesso!");
        } catch (Exception ex) {
            jLabel_Mesagem.setText(ex.getMessage());
        }

    }//GEN-LAST:event_jButton_OkActionPerformed

    private void jButton_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelarActionPerformed
        // TODO add your handling code here:

        int cancelar = JOptionPane.showConfirmDialog(null, "Deseja cancelar ?");
        if (cancelar == JOptionPane.YES_OPTION) {
            dispose();
        } 
    }//GEN-LAST:event_jButton_CancelarActionPerformed

    private void id_permissao_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_id_permissao_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_id_permissao_usuarioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CadPermissaoUsuario(1).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField id_permissao_usuario;
    private javax.swing.JButton jButton_Cancelar;
    private javax.swing.JButton jButton_Ok;
    private javax.swing.JComboBox jComboBox_idFuncionario;
    private javax.swing.JComboBox jComboBox_idPermissao;
    private javax.swing.JLabel jLabel_Codigo;
    private javax.swing.JLabel jLabel_Funcionario;
    private javax.swing.JLabel jLabel_Mesagem;
    private javax.swing.JLabel jLabel_Permissao;
    // End of variables declaration//GEN-END:variables
}
